/**********************************************************************/
/*
Author: Karan Makkar
Created: Nov 2023
Updated: Aug 2025, by Youssef Assarssah
Description: Balance Table by Treated in Batch in Old National Survey Data
Output: TableA5
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_balance_sussak_pwin_det.txt", text replace

* SE CLUSTERING (OLS): Individual level
global vce vce(cluster anon_id4)

* Varlists

global main_vars_ind totcons ///
                employed ///
                self_emp_bus_owner ///
                hours_worked ///
                hourly_adj_wage ///
                ind_adj_earnings ///
                looking_job ///
                prep_new_bus

global durvars_sus dur_canister ///
            dur_fridge ///  
            dur_aircon ///
            dur_waterheat ///
            dur_phone ///
            dur_computer ///
            dur_gold ///
            dur_bike ///
            dur_boat ///
            dur_motorboat ///
            dur_car ///
            dur_tv ///
            dur_land 

global morevars_sus use_internet ///
            use_internet_bank ///
            receive_KKS /// 
            receive_pkh_year ///
            ever_BPNT ///
            r_assist ///
            nr_assist
            
global morevars_sak perm_worker ///
            temp_worker ///
            family_unpaid ///
            multiple_jobs ///
            use_internet_work ///
            looking_job

* Filepath globasl
global mar18 "$KP_deid_susenas/Clean/sus_mar18_deid_clean_merged.dta"
global mar19 "$KP_deid_susenas/Clean/sus_mar19_deid_clean_merged.dta"
global mar20 "$KP_deid_susenas/Clean/sus_mar20_deid_clean_merged.dta"
global sep18 "$KP_deid_susenas/Clean/sus_sep18_deid_clean_merged.dta"
global feb19 "$KP_deid_sakernas/Clean/sak_feb19_deid_clean_merged.dta"
global feb20 "$KP_deid_sakernas/Clean/sak_feb20_deid_clean_merged.dta"
global aug18 "$KP_deid_sakernas/Clean/sak_aug18_deid_clean_merged.dta"
global aug19 "$KP_deid_sakernas/Clean/sak_aug19_deid_clean_merged.dta"

* Include makestars program
include "$KP_do/3 Functions/makestars.do"

* Regression programs
include "$KP_do/3 Functions/impact_regression_programs"

* Table formatting and flipping programs
include "$KP_do/3 Functions/impact_table_programs"

/*----------------------------------------------------*/
* Section: Load and Clean Data
/*----------------------------------------------------*/

* Import SUS/SAK data 
u "${mar18}", clear
append using "${mar19}", force
append using "${mar20}", force
append using "${sep18}", force
append using "${feb19}", force
append using "${feb20}", force
append using "${aug18}", force
append using "${aug19}", force


* Drop non-randomized batches
drop if inlist(batch, 1, 15)

* Consumption rename
replace tot_adj_cons = cons_adj if inlist(sus_round, 1, 3)
gen totcons = tot_adj_cons*hh_size
replace hours_worked = hrs_worked_all if !mi(sus_round)
gen foodcons = cons_adj_food*hh_size
gen nfoodcons = cons_adj_nfood*hh_size

gen looking_jobb = looking_job

*Education cat
recode education (1/2 = 1) (3 = 2) (4/8 = 3), gen(ed_cat) 

* Application number
bysort anon_id4 sus_round sak_round: gen totapp = _N

gen constant = 0
gen missing = .

replace totcons = totcons/1000 if inlist(sus_round, 1, 3)
replace ind_adj_earnings = ind_adj_earnings/1000
replace hourly_adj_wage = hourly_adj_wage/1000

gen x = win_in_batch

/*----------------------------------------------------------------*/
* Section: OLS Regressions
/*----------------------------------------------------------------*/
* Declare wave-specific specifications
global columns ols_sak_st ols_sak_1 ols_sak_2 ols_sak_3 ols_sak_4 ols_sus_st ols_sus_1 ols_sus_2 ols_sus_3 ols_sus_4 
global column_num 10

* Declare empty model and variable lists
global model_list
global var_list

* Declare variable list for table
global vars "${main_vars_ind}"
 
* loop through each variable of table
foreach y of varlist $vars {

* Run OLS regression (actual win)
    
* Sak Regs
    summ `y' if sak_round == 4
    if r(N) != 0 {
        ols_reg `y'_sak_st `y' x "" "!mi(sak_round)" "absorb(c.stratum_win_prob#batch#sak_round sak_round#batch)" "$vce"
        ols_reg `y'_sak_1 `y' x "" "sak_round ==1" "absorb(c.stratum_win_prob#batch batch)" "$vce"
        ols_reg `y'_sak_2 `y' x "" "sak_round ==2" "absorb(c.stratum_win_prob#batch batch)" "$vce"
        ols_reg `y'_sak_3 `y' x "" "sak_round ==3" "absorb(c.stratum_win_prob#batch batch)" "$vce"
        ols_reg `y'_sak_4 `y' x "" "sak_round ==4" "absorb(c.stratum_win_prob#batch batch)" "$vce"
    }
    
    summ `y' if sak_round == 4
    if r(N) == 0 {
        constant_reg `y'_sak_st
        constant_reg `y'_sak_1
        constant_reg `y'_sak_2
        constant_reg `y'_sak_3
        constant_reg `y'_sak_4
    }

    *Sus Regs
    summ `y' if sus_round ==4
    if r(N) != 0 {
        ols_reg `y'_sus_st `y' x "" "!mi(sus_round)"  "absorb(c.stratum_win_prob#batch#sus_round sus_round#batch)" "$vce"
        ols_reg `y'_sus_1 `y' x "" "sus_round ==1" "absorb(c.stratum_win_prob#batch batch)" "$vce"
        ols_reg `y'_sus_2 `y' x "" "sus_round ==2" "absorb(c.stratum_win_prob#batch batch)" "$vce"
        ols_reg `y'_sus_3 `y' x "" "sus_round ==3" "absorb(c.stratum_win_prob#batch batch)" "$vce"
        ols_reg `y'_sus_4 `y' x "" "sus_round ==4" "absorb(c.stratum_win_prob#batch batch)" "$vce"            
    }
    
    summ `y' if sus_round ==4
    if r(N) == 0 {
        constant_reg `y'_sus_st
        constant_reg `y'_sus_1
        constant_reg `y'_sus_2
        constant_reg `y'_sus_3 
        constant_reg `y'_sus_4
    }

    * Add var to list
    global var_list $var_list `y'
    * Add models to list
    global model_list $model_list `y'_sak_st `y'_sak_1 `y'_sak_2 `y'_sak_3 `y'_sak_4 `y'_sus_st `y'_sus_1 `y'_sus_2 `y'_sus_3 `y'_sus_4

}

* Flip table and export table
save_sterfile "$KP_output/tables/descriptive/sterfiles/TableA5.ster"       
flip_table x "$var_list" "$model_list" "$columns" "$column_num"
export_table "$columns" "$KP_output/tables/descriptive/tex/TableA5" totcons

*---------------------------------------------------
* Add Joint Test F-Stats for the stacked regressions
*---------------------------------------------------

// Run the regression-based joint test of balance in the stacked waves

* Sakernas
global sak_vars employed ///
                self_emp_bus_owner ///
                hours_worked ///
                hourly_adj_wage ///
                ind_adj_earnings ///
                looking_job ///
                prep_new_bus
reghdfe win_in_batch $sak_vars if !mi(sak_round), absorb(c.stratum_win_prob#batch#sak_round sak_round#batch) $vce
	 
test $sak_vars
local fstr_sk  = string(r(F), "%4.2f")
di "`fstr'"
scalar pval_sk = r(p)
local stars_sk = cond(pval_sk < 0.01, "***", cond(pval_sk < 0.05, "**", cond(pval_sk < 0.10, "*", "")))

* Susenas
global sus_vars totcons employed ///
                self_emp_bus_owner ///
                hours_worked
reghdfe  win_in_batch $sus_vars if !mi(sus_round), absorb(c.stratum_win_prob#batch batch) $vce
test $sus_vars

local fstr_ss  = string(r(F), "%4.2f")
di "`fstr'"
scalar pval_ss = r(p)
local stars_ss = cond(pval_ss < 0.01, "***", cond(pval_ss < 0.05, "**", cond(pval_ss < 0.10, "*", "")))

* Pst write F-Stats in the tex file
cap file close fin
cap file close fout
file open fin using "$KP_output/tables/descriptive/tex/TableA5.tex", read
file open fout using "$KP_output/tables/descriptive/tex/TableA5.tex", write replace

file read fin line
* Loop through original file
while (r(eof)==0) {
  

    * When Observations row is found, insert Joint F-stat before it
    if strpos("`line'", "Observations") > 0 {
        * Insert custom row with shared F-stat
        file write fout "F-stat (joint test) & `fstr_sk'`stars_sk' & & & & & `fstr_ss'`stars_ss' & & & & \\" _n
    }
    
    * Write the original line (including Observations)
    file write fout "`line'" _n
    file read fin line
}

file close fin
file close fout